package ynu.edu.elmspringboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.elmspringboot.po.DeliveryAddress;
import ynu.edu.elmspringboot.service.DeliveryAddressService;
import ynu.edu.elmspringboot.utils.PageResult;

import java.util.List;

@RestController
@RequestMapping("/DeliveryAddressController")
@Api(tags = "收货地址管理")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @ApiOperation("根据用户id查询收货地址信息")
    @PostMapping("/listDeliveryAddressByUserId")
    public List<DeliveryAddress> listDeliveryAddressByUserId(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.listDeliveryAddressByUserId(deliveryAddress.getUserId());
        return deliveryAddressService.lambdaQuery().eq(DeliveryAddress::getUserId, deliveryAddress.getUserId()).list();
    }

    @ApiOperation("根据收货地址id查询收货地址信息")
    @PostMapping("/getDeliveryAddressById")
    public DeliveryAddress getDeliveryAddressById(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.getDeliveryAddressById(deliveryAddress.getDaId());
        return deliveryAddressService.getById(deliveryAddress.getDaId());
    }

    @ApiOperation("保存收货地址信息")
    @PostMapping("/saveDeliveryAddress")
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.saveDeliveryAddress(deliveryAddress);
        return deliveryAddressService.save(deliveryAddress) ? 1 : 0;
    }

    @ApiOperation("更新收货地址信息")
    @PostMapping("/updateDeliveryAddress")
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        return deliveryAddressService.updateById(deliveryAddress) ? 1 : 0;
    }

    @ApiOperation("删除收货地址信息")
    @PostMapping("/removeDeliveryAddress")
    public int removeDeliveryAddress(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.removeDeliveryAddress(deliveryAddress.getDaId());
        return deliveryAddressService.removeById(deliveryAddress.getDaId()) ? 1 : 0;
    }

    @ApiOperation("分页查询用户收货地址")
    @GetMapping("/getDeliveryAddressPage")
    public PageResult<DeliveryAddress> getDeliveryAddressPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("用户ID") @RequestParam String userId,
            @ApiParam("联系人姓名") @RequestParam(required = false) String contactName) {
        
        Page<DeliveryAddress> page = new Page<>(current, size);
        IPage<DeliveryAddress> result = deliveryAddressService.getDeliveryAddressPage(page, userId, contactName);
        return PageResult.fromIPage(result);
    }

    @ApiOperation("根据用户ID分页查询收货地址")
    @GetMapping("/getDeliveryAddressPageByUser")
    public PageResult<DeliveryAddress> getDeliveryAddressPageByUser(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("用户ID") @RequestParam String userId) {
        
        Page<DeliveryAddress> page = new Page<>(current, size);
        IPage<DeliveryAddress> result = deliveryAddressService.getDeliveryAddressPageByUser(page, userId);
        return PageResult.fromIPage(result);
    }
}
